package org.lc.stk.security.util;

import java.util.ArrayList;
import java.util.List;

import org.lc.stk.model.user.permission.CoursePermission;
import org.lc.stk.model.user.permission.Permission;
import org.lc.stk.model.user.permission.QuestionBankPermission;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

/**
 * 权限转换工具类
 * 将权限对象转换为Spring Security所需的SimpleGrantedAuthority
 */
public class PermissionConverter {

    private static final String COURSE_PREFIX = "course:";
    private static final String BANK_PREFIX = "bank:";
    private static final String PAPER_PREFIX = "paper:";
    
    public static List<SimpleGrantedAuthority> convertToAuthorities(Permission permission) {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        String resourceId = permission.getResourceId() != null ? permission.getResourceId() : "*";

        if (permission instanceof CoursePermission) {
            addCoursePermissions((CoursePermission) permission, resourceId, authorities);
        } else if (permission instanceof QuestionBankPermission) {
            addQuestionBankPermissions((QuestionBankPermission) permission, resourceId, authorities);
        }

        return authorities;
    }

    private static void addCoursePermissions(CoursePermission permission, String resourceId, List<SimpleGrantedAuthority> authorities) {
        addPermissionIfTrue(permission.getCanCreate(), COURSE_PREFIX + resourceId + ":create", authorities);
        addPermissionIfTrue(permission.getCanEdit(), COURSE_PREFIX + resourceId + ":edit", authorities);
        addPermissionIfTrue(permission.getCanDelete(), COURSE_PREFIX + resourceId + ":delete", authorities);
        addPermissionIfTrue(permission.getCanView(), COURSE_PREFIX + resourceId + ":view", authorities);
        addPermissionIfTrue(permission.getCanManageChapters(), COURSE_PREFIX + resourceId + ":manage_chapters", authorities);
        addPermissionIfTrue(permission.getCanManageKnowledgePoints(), COURSE_PREFIX + resourceId + ":manage_knowledge_points", authorities);
    }

    private static void addQuestionBankPermissions(QuestionBankPermission permission, String resourceId, List<SimpleGrantedAuthority> authorities) {
        // 题库权限
        addPermissionIfTrue(permission.getCanCreateBank(), BANK_PREFIX + resourceId + ":create", authorities);
        addPermissionIfTrue(permission.getCanManageBank(), BANK_PREFIX + resourceId + ":manage", authorities);
        addPermissionIfTrue(permission.getCanViewBank(), BANK_PREFIX + resourceId + ":view", authorities);
        addPermissionIfTrue(permission.getCanAddQuestion(), BANK_PREFIX + resourceId + ":add_question", authorities);
        addPermissionIfTrue(permission.getCanEditQuestion(), BANK_PREFIX + resourceId + ":edit_question", authorities);
        addPermissionIfTrue(permission.getCanDeleteQuestion(), BANK_PREFIX + resourceId + ":delete_question", authorities);
        addPermissionIfTrue(permission.getCanReviewQuestion(), BANK_PREFIX + resourceId + ":review_question", authorities);

        // 试卷权限
        addPermissionIfTrue(permission.getCanCreatePaper(), PAPER_PREFIX + resourceId + ":create", authorities);
        addPermissionIfTrue(permission.getCanManagePaper(), PAPER_PREFIX + resourceId + ":manage", authorities);
        addPermissionIfTrue(permission.getCanReviewPaper(), PAPER_PREFIX + resourceId + ":review", authorities);
    }

    private static void addPermissionIfTrue(Boolean permissionFlag, String permission, List<SimpleGrantedAuthority> authorities) {
        if (Boolean.TRUE.equals(permissionFlag)) {
            authorities.add(new SimpleGrantedAuthority(permission));
        }
    }
}